package com.bytedy.sboot.support;

import java.io.IOException;
import java.io.PrintWriter;

import com.bytedy.sboot.util.JsonUtil;
import org.springframework.http.HttpStatus;
import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ResultWriter {

    public static void writeResp(HttpServletResponse response, Object output) throws IOException {
        log.info("the output: {}", output);

        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        PrintWriter out = response.getWriter();
        if(output instanceof String op) {
            out.write(op);
        } else {
            out.write(JsonUtil.toJson(output));
        }

    }

}
